<template>
  <div class="logistics_list">
    <div class="list_choose">
      <div class="list_choose_time" @click="statusChoose(chooseStatus)">
        {{ chooseStatus || '状态' }}
        <span></span>
      </div>
      <div class="list_choose_time" @click="timeChooseYear(chooseYear)">
        {{ chooseYear || '年份' }}
        <span></span>
      </div>
      <div class="list_choose_time" @click="timeChooseMonth(chooseMonth)" v-if="chooseYear !== '全部'">
        {{ chooseMonth || '月份' }}
        <span></span>
      </div>
      <van-button class="btn" size="mini" type="info" @click="goSure()" v-if="isApplyRole && firstType === '1'">
        批量确定
      </van-button>
    </div>
    <div class="list_con">
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="listDate.length > 0 ? '没有更多了' : ''"
        @load="getData"
        :offset="300"
        :immediate-check="true">
        <div class="list_box" v-if="listDate.length > 0">
          <div class="list_info" @click="goDetail(item)" v-for="item in listDate" :key="item.id">
            <div class="top">
              <div class="info_name">
                {{ item.title }}
              </div>
              <div class="info_time">{{ item.createTime }}</div>
            </div>
            <div class="center">
              <div class="info_org">{{ item.orgName }}</div>
              <div class="info_status" :class="{ is_ready: item.status === '0' }">{{ item.status | listStatus }}</div>
            </div>
          </div>
        </div>
        <van-empty v-else description="暂无数据" />
      </van-list>
    </div>
    <!-- 底部保存按钮 -->
    <div class="addNew" @click="goToAdd" v-if="firstType == '0' && (isOtherRole || isNormalRole || isOrgRole)">
      <img src="@/assets/images/common/add_new.png" alt="" />
    </div>
    <van-popup v-model="showTimeYear" position="bottom">
      <van-picker
        title="选择年份"
        show-toolbar
        :columns="yearOption"
        @confirm="timeConfirmYear"
        @cancel="showTimeYear = false"></van-picker>
    </van-popup>
    <van-popup v-model="showTimeMonth" position="bottom">
      <van-picker
        title="选择月份"
        show-toolbar
        :columns="monthOption"
        @confirm="timeConfirmMonth"
        @cancel="showTimeMonth = false"></van-picker>
    </van-popup>

    <van-popup class="popu_picker" v-model="showTypes" position="bottom">
      <van-picker
        show-toolbar
        visible-item-count="4"
        :columns="statusOptions"
        default-index="0"
        @cancel="showTypes = false"
        @confirm="onConfirmY" />
    </van-popup>
  </div>
</template>

<script>
import {
  getlistLogicticsApplys,
  getlistLogicticsConfirm,
  checkBatchConfirmAuthority,
  approveBatch,
} from '@api/api-dailyManage.js'
import { mapGetters } from 'vuex'
import { selectYearFun } from '@common/utility'
export default {
  name: 'logisticsList',
  components: {},
  props: {
    firstType: {
      type: String,
      default: '',
    },
  },
  filters: {
    listStatus(val) {
      switch (val) {
        case '0':
          return '待审批'
        case '1':
          return '审批通过'
        case '2':
          return '审批不通过'
        case '3':
          return '暂存'

        default:
          break
      }
    },
  },
  data() {
    return {
      tablePage: {
        currentPage: 1,
        pageSize: 10,
        totalResult: 0,
      },
      userInfo: JSON.parse(localStorage.userInfo),
      yearOption: [
        { text: '2023年', value: '2023' },
        { text: '2024年', value: '2024' },
      ],
      yearData: '',
      monthOption: [
        { text: '全部', value: '' },
        { text: '1月', value: '1' },
        { text: '2月', value: '2' },
        { text: '3月', value: '3' },
        { text: '4月', value: '4' },
        { text: '5月', value: '5' },
        { text: '6月', value: '6' },
        { text: '7月', value: '7' },
        { text: '8月', value: '8' },
        { text: '9月', value: '9' },
        { text: '10月', value: '10' },
        { text: '11月', value: '11' },
        { text: '12月', value: '12' },
      ],
      monthData: '',
      statusOptions: [
        {
          value: '',
          text: '全部',
        },
        {
          value: '0',
          text: '待审批',
        },
        {
          value: '1',
          text: '审批通过',
        },
        {
          value: '2',
          text: '审批不通过',
        },
      ],
      statusData: '',
      chooseYear: '全部',
      chooseMonth: '全部',
      chooseStatus: '全部',
      listDate: [],
      loading: false,
      finished: false,
      showTimeYear: false,
      showTypes: false,
      showTimeMonth: false,
      selectYearFun,
    }
  },
  computed: {
    ...mapGetters('role', ['isOtherRole', 'isNormalRole', 'isOrgRole', 'isApplyRole']),
  },
  watch: {
    firstType: {
      handler(news) {
        if (news) {
          this.statusData = ''
          this.yearData = ''
          this.monthData = ''
          this.tablePage.currentPage = '1'
          this.listDate = []
          this.finished = false
          if (news === '0') {
            this.getlistLogicticsApplys()
          } else {
            this.getlistLogicticsConfirm()
          }
        }
      },
    },
  },
  created() {
    this.yearOption = this.selectYearFun(2023)
    this.yearOption.unshift({ text: '全部', value: '' })
  },
  mounted() {},
  methods: {
    getData() {
      if (this.firstType === '0') {
        this.getlistLogicticsApplys()
      } else {
        this.getlistLogicticsConfirm()
      }
    },
    // 获取数据列表
    getlistLogicticsApplys() {
      this.loading = true
      const serarh = {
        current: this.tablePage.currentPage,
        size: this.tablePage.pageSize,
        orgId: this.userInfo.loginPostInfo.orgId,
        status: this.statusData,
        year: this.yearData,
        month: this.monthData,
      }
      getlistLogicticsApplys(serarh).then(res => {
        if (res.code === 0) {
          this.listDate.push(...res.data.records)
          this.tablePage.currentPage++
          this.tablePage.totalResult = res.data.total
          this.loading = false
          if (this.listDate.length === this.tablePage.totalResult) {
            this.finished = true
          }
        }
      })
    },
    // 获取数据列表
    getlistLogicticsConfirm() {
      this.loading = true
      const param = {
        id: this.$route.query.id || '',
        month: this.monthData,
        year: this.yearData,
        current: this.tablePage.currentPage,
        size: this.tablePage.pageSize,
        orgId: this.userInfo.loginPostInfo.orgId,
        status: this.statusData,
      }
      getlistLogicticsConfirm(param).then(res => {
        if (res.code === 0) {
          this.listDate.push(...res.data.records)
          this.tablePage.currentPage++
          this.tablePage.totalResult = res.data.total
          this.loading = false
          if (this.listDate.length === this.tablePage.totalResult) {
            this.finished = true
          }
        }
      })
    },
    // 登记列表跳转详情
    goDetail(item) {
      this.$router.push({
        path: '/dailyDetail',
        query: {
          id: item.id,
          status: item.status,
          processInstanceId: item.processInstanceId,
          taskId: item.taskId,
          firstType: this.firstType,
          active: '0',
        },
      })
    },
    // 选择时间
    timeChooseYear(time) {
      this.showTimeYear = true
    },
    timeChooseMonth(time) {
      this.showTimeMonth = true
    },
    // 时间确定
    timeConfirmYear(val) {
      this.showTimeYear = false
      this.chooseYear = val.text
      this.yearData = val.value
      if (this.chooseYear === '全部') {
        this.chooseMonth = '全部'
        this.monthData = ''
      }
      this.resizeList()
    },
    timeConfirmMonth(val) {
      this.showTimeMonth = false
      this.chooseMonth = val.text
      this.monthData = val.value
      this.resizeList()
    },
    resizeList() {
      this.tablePage.currentPage = 1
      this.listDate = []
      this.finished = false
      this.getData()
    },

    goToAdd() {
      this.$router.push({ path: '/dailyAdd', query: { active: '0' } })
    },
    onConfirmY(val) {
      this.showTypes = false
      this.chooseStatus = val.text
      this.statusData = val.value
      this.resizeList()
    },
    statusChoose() {
      this.showTypes = true
    },
    goSure() {
      checkBatchConfirmAuthority().then(res => {
        if (res.code === 0 && res.data === '0') {
          this.$dialog
            .confirm({
              title: '提示',
              message: '物品已确认发放完毕，暂无确认单',
              showCancelButton: false,
            })
            .then(() => {})
        } else {
          this.$dialog
            .confirm({
              title: '提示',
              message: '请确认当月物品已发放完毕，确定后不可修改。',
            })
            .then(() => {
              approveBatch().then(res => {
                if (res.code === 0) {
                  this.$toast.success('确定成功')
                  this.resizeList()
                }
              })
            })
            .catch(() => {})
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.logistics_list {
  font-size: 14px;

  /deep/ .van-popup {
    position: absolute;
  }
  /deep/ .van-overlay {
    position: absolute;
  }

  .list_choose {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 36px;
    background-color: #ffffff;
    position: relative;
    padding: 0 10px;
    border-bottom: 1px solid #f2f2f2;
    .list_choose_time {
      display: flex;
      align-items: center;
      margin-left: 5px;
      margin-right: 10px;
      span {
        width: 0;
        height: 0;
        border-top: 4px solid #dcdee0;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        margin-left: 5px;
      }
    }
  }
  .list_con {
    padding: 7px 10px;
    font-size: 14px;
    .list_info {
      padding: 7px;
      border-bottom: 1px solid #efefef;
      .top,
      .center {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .top {
        .info_time {
          font-size: 13px;
        }
      }
      .center {
        padding-top: 7px;
        .info_org {
          background-color: #f9f1e8;
          border-radius: 2px;
          padding: 3px 8px;
          color: #a57d60;
        }
        .info_status {
          color: #ee792f;
        }
        .is_ready {
          color: #d9001b;
        }
      }
    }
  }
  // 底部
  .addNew {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 50px;
    width: 48px;
    height: 48px;
    img {
      width: 100%;
      opacity: 0.7;
    }
  }
}
</style>
